package org.facyt.commons.view.field.container;

import com.vaadin.data.Property;
import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.data.util.ObjectProperty;

public class SelectItemContainer<T> extends BeanItemContainer<T> {

	private static final long serialVersionUID = 8597673278283048550L;

	private String message;

	private T emptyValue;

	public SelectItemContainer(Class<? super T> type, String message) {
		super(type);
		init(type, message);
	}

	@SuppressWarnings("unchecked")
	protected void init(Class<? super T> type, String message) {
		try {
			this.emptyValue = (T) type.newInstance();
			addBean(emptyValue);
			this.message = message;
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}

	@Override
	public Property getContainerProperty(Object itemId, Object propertyId) {

		Property property = null;

		if (emptyValue.equals(itemId)) {
			property = new ObjectProperty<String>(message);
		} else {
			property = super.getContainerProperty(itemId, propertyId);
		}

		return property;
	}

	public T getEmptyValue() {
		return emptyValue;
	}

}
